<template>
  <div class="login_body">
    <div class="login">
      <div class="container a-container" id="a-container">
        <el-form :model="user" class="form" id="b-from">
          <el-form-item>
            <h2 class="form_title title">欢迎登录</h2>
          </el-form-item>
          <el-form-item>
            <el-input v-model="user.account" class="form_input" placeholder="请输入账号"></el-input>
          </el-form-item>
          <el-form-item>
            <el-input v-model="user.password" class="form_input" type="password" placeholder="请输入密码"></el-input>
          </el-form-item>

          <el-form-item>
            <el-select v-model="user.identity" placeholder="请选择用户身份" class="form_input" style="margin-left: 15px; width: 87%;">
              <el-option label="管理员" value="0"></el-option>
              <el-option label="普通用户" value="1"></el-option>
              <el-option label="商家" value="2"></el-option>
            </el-select>
          </el-form-item>

          <el-form-item>
            <a href="" class="form_link" style="color: #a0a5a8;" @click="forget()">忘记密码?</a>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" size="medium" @click="login()" round>Login</el-button>
          </el-form-item>
        </el-form>
      </div>

      <div class="switch" id="switch-cnt">
        <div class="switch_circle"></div>
        <div class="switch_circle switch_circle-t"></div>

        <div class="switch_container" id="switch-c1">
          <h2 class="switch_title title" style="letter-spacing: 0;">你 好, 朋 友</h2>
          <p class="switch_description description">没有账号吗?注册一个吧</p>
          <div>
            <el-button type="primary" size="medium" @click="register" round>注册用户</el-button>
            <el-button type="primary" size="medium" @click="registershopper" round>注册商家</el-button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style>
@import "@/assets/loginandregister.css";
</style>

<script>

import request from "@/utils/request";

export default {
  name: "Login",
  data() {
    return {
      region:'',
      user: {},
      idenity: '',
    }
  },
  // 页面加载的时候，做一些事情，在created里面
  created() {

  },
  // 定义一些页面上控件出发的事件调用的方法
  methods: {
    login() {
      request.post("/user/login",this.user).then(res => {
        if (res.code === '0' && res.data.identity === 0){
          this.$message({
            message: '登录成功',
            type: 'success'
          });
          sessionStorage.setItem("user",JSON.stringify(res.data));
          this.$router.push("/adminview")
        }
        else if (res.code === '0' && res.data.identity === 1){
          this.$message({
            message: '登录成功',
            type: 'success'
          });
          sessionStorage.setItem("user",JSON.stringify(res.data));
          this.$router.push("/userview")
        }
        else if (res.code === '0'&& res.data.identity === 2){
          this.$message({
            message: '登录成功',
            type: 'success'
          });
          sessionStorage.setItem("user",JSON.stringify(res.data));
          this.$router.push("/businessview")
        }
        else {
          this.$message({
            message: res.msg,
            type: 'error'
          });
          this.$router.push("/loginview")
        }

      })
    },
    register() {
      this.$router.push("/registerview")
    },
    registershopper(){
      this.$router.push("/registershopperview")
    },
    forget() {
      this.$router.push("/forgetpwdview")
    }
  }
}
</script>

